/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.plugins;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.grayrabbit.cms.entity.SubjectOption;
import org.grayrabbit.cms.entity.SurveySubject;
import org.grayrabbit.cms.service.plugins.SubjectOptionService;
import org.grayrabbit.cms.vo.plugins.SubjectOptionBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.web.struts2.GenericAction;

/**
 * <b>SubjectOptionAction</b> Struts2 action�?





 * @version 1.0,创建时间:2011-05-16 16:09:05 
 */
@Controller
@Scope("prototype")
public class SubjectOptionAction extends GenericAction<SubjectOptionBean> {
	private static final long serialVersionUID = -1L;
	//Service接口接入
	@Autowired
	@Qualifier("subjectOptionServiceImpl")
	private SubjectOptionService subjectOptionService;
	
	//VO对象，由前端页面数据组装
	private SubjectOptionBean subjectOption;
	
	private SurveySubject surveySubject;
	
	/**return 前端页面数据组装VO对象,<b>SubjectOptionBean</b>*/
	public SubjectOptionBean getSubjectOption() {
		return subjectOption;
	}

	public void setSubjectOption(SubjectOptionBean subjectOption) {
		this.subjectOption = subjectOption;
	}
	
	//分页对象
	private Page<SubjectOption> page = new Page<SubjectOption>(MAX_PAGE_NUM,true);
	
	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<SubjectOption> getPage() {
		return page;
	}
	//接收前端form,url提交参数组装成分页property
	public void setPage(Page<SubjectOption> page) {
		this.page = page;
	}
	
	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList(){
		prepareModel();
	}
	
	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare(){
		if(subjectOption==null){
			subjectOption = new SubjectOptionBean();
		}
	}
	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法,
	 *  如：
	 *	<pre>
	 *		<code> name="user.userName"</code>
	 *	改写成：
	 *		<code> name="userName"</code>
	 *	action实例中，会自动组成user成员变量
	 *	</pre>
	 */
	public SubjectOptionBean getModel() {
		return subjectOption;
	}
	
	@Override
	protected void prepareModel(){
		if(StringUtils.isNotBlank(subjectOption.getOptionId())){
			subjectOption = (SubjectOptionBean)dozer.map(subjectOptionService.getSubjectOption(subjectOption.getOptionId()),SubjectOptionBean.class);
		}
		
	}

	/**
	 * 删除<b>SubjectOption</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到subjectoption-list.action
	 */
	@Override
	@Action(value="subjectoption-delete",
			results={@Result(name="reload",type="redirectAction",params={"actionName","subjectoption-list"})}
			)
	public String delete(){
		subjectOptionService.delete(key);
		return RELOAD;
	}
	
	/**
	 * SubjectOption的列表方法，通过成员变量<b>page�?{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到subjectoption-list.jsp页面
	 */
	@Override
	@Action(value="subjectoption-list")
	public String list(){
		subjectOptionService.list(page,subjectOption);
		return SUCCESS;
	}
	/**
	 * SubjectOption的新增与编辑的方法执行成功能
	 * 将映射到subjectoption-input.jsp
	 */
	@Action(value="subjectoption-input")
	public String input(){
		prepareModel();
		return SUCCESS;
	}
	
	/**
	 * save or update SubjectOption的方法，将成员property subjectOption转成SubjectOption实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到subjectoption-list.action
	 */
	@Override
	@Action(value="subjectoption-save",
			results={@Result(name="reload",type="redirectAction",params={"actionName","subjectoption-list"})}
		)
	public String save(){
		SubjectOption obj = (SubjectOption) dozer.map(subjectOption, SubjectOption.class);
		if(!StringUtils.isNotBlank(obj.getOptionId())){
			obj.setOptionId(null);
		}
		subjectOptionService.save(obj);
		return RELOAD;
	}
	/**
	 * 根据科目Id查询答案项
	 * @return
	 */
	@Action(value="subjectoption-getOption")
	public String findBySubjectId(){
		List<SubjectOption> list = subjectOptionService.findBySubjectId(surveySubject.getSubjectId());
		Map<String,Object> map =new HashMap<String,Object>(); 
		 JSONObject json = new  JSONObject();
		 JSONArray jsonArray = new JSONArray();
		for(int i=0;i<list.size();i++){
			map.put("optionTitle", list.get(i).getOptionTitle());
			map.put("optionId", list.get(i).getOptionId());
			json = JSONObject.fromObject( map ); 
			jsonArray.add(json);
		}
		return renderText(jsonArray.toString());
	}

	public SurveySubject getSurveySubject() {
		return surveySubject;
	}

	public void setSurveySubject(SurveySubject surveySubject) {
		this.surveySubject = surveySubject;
	}
	
}